<template>
  <div class="login-page">
    <div class="center-box">
      <div class="left-box">
        <img src="../../assets/images/login-left.jpg" alt=""></img>
      </div>
      <div class="right-box">
        <div class="top">计算机应用技术3班</div>
        <!-- <div class="center">
           <el-input
            v-model="username"
            placeholder="请输入用户名"
            :prefix-icon="User"
          />
          <el-input
            v-model="password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
          />
          <el-button type="primary" :loading="loading" @click="loginAction">登 录</el-button>
        </div> -->
          <el-form
            ref="ruleFormRef"
            style="max-width: 600px"
            :model="resisterForm"
            status-icon
            :rules="rules"
            label-width="auto"
            class="center"
          >
            <el-form-item label="用户名" prop="username">
              <el-input v-model="resisterForm.username" type="text" autocomplete="off" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="resisterForm.password"
                type="password"
                autocomplete="off"
              />
            </el-form-item>
            <el-form-item label="确认密码" prop="password2">
              <el-input
                v-model="resisterForm.password2"
                type="password"
                autocomplete="off"
              />
            </el-form-item>
            <el-button type="primary" @click="registerAction">注 册</el-button>
        </el-form>    
        <div class="bottom">
          <span>已有账号！</span>
          <span @click="$router.push('/login')">去登录</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { validateAccountOrPwd } from '@/utils/validator.js';
import axios from 'axios';
import { ElMessage } from 'element-plus';
import { ref } from 'vue';

const resisterForm = ref({
  username: '',
  password: '',
  password2: ''
})

const ruleFormRef = ref(null)

const usernameCheck = (rule, value, callback) => {
  const res =  validateAccountOrPwd(value)
  if (res.valid) {
    callback()
  } else {
    callback(new Error(res.message))
  }
}

const pwdCheck = (rule, value, callback) => {
   const res =  validateAccountOrPwd(value)
    if (res.valid) {
      callback()
    } else {
      callback(new Error(res.message))
    }
}

const pwdCheck2 = (rule, value, callback) => {
  if (value === resisterForm.value.password) {
    callback()
  } else {
    callback(new Error('两次密码不一致'))
  }
}

const rules = ref({
  username: [{ validator: usernameCheck, trigger: 'blur' }],
  password: [{ validator: pwdCheck, trigger: 'blur' }],
  password2: [{ validator: pwdCheck2, trigger: 'blur' }],
})

const registerAction = () => {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      console.log('验证通过')
      // 注册操作
      axios.post('http://152.136.202.150:3000/api/auth/register', {
        username: resisterForm.value.username,
        password: resisterForm.value.password
      }).then(res => {
        console.log(res)
        if (res.status < 300) {
          ElMessage.success('注册成功')
        } else {
          ElMessage.success(res.data.message)
        }
      }).catch(err => {
        console.log(err);
      })
    } else {
      console.log('验证失败')
    }
  })
}

</script>

<style lang="scss" scoped>
// background-color：背景颜色（如 #fff、rgba(0,0,0,0.5)）
// background-image：背景图片（如 url(...)）
// background-repeat：图片重复方式（repeat/no-repeat/repeat-x/repeat-y）
// background-position：图片位置（如 center、top left、50% 50%）
// background-size：图片大小（如 cover、contain、100% 100%）
// background-attachment：滚动行为（scroll/fixed/local）
// background-origin/background-clip：背景定位 / 绘制区域（较少用）
.login-page {
  width: 100vw;
  height: 100vh;
  background-image: url(../../assets/images/login-bg.png);
  background-repeat: no-repeat;
  background-position: center center;
  background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .center-box {
    width: 800px;
    height: 400px;
    display: flex;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 3px 4px rgba(0, 0, 0, 0.2);
    &:hover {
      transform: scale(1.02);
      box-shadow: 0 3px 4px rgba(45, 10, 125, 0.4);
    }
    .left-box {
      flex: 1;
      img {
        height: 100%;
      }
    }
    .right-box {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      background-color: #fafafa;
      padding: 16px;
      .top {
        flex: 1;
        text-align: center;
        padding: 16px 0;
        font-size: 24px;
        font-weight: 700;
        color: #409EFF;
      }
      .center {
        flex: 4;
        display: flex;
        flex-direction: column;
        justify-content: space-evenly;
      }
      .bottom {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        span {
          margin: 0 8px;
          font-size: 12px;
          color: #999;
          &:last-child {
            color: #409EFF;
            cursor: pointer;
          }
        }
      }
    }
  }
}
</style>